<template>
    <div class="block">
        <div>
            <exSelect v-model="devices" label="设备列表" :dictList="deviceList" :multiple="false" collapse-tags size="mini" elstyle="width:330px"></exSelect>
            <el-button type="primary" @click="getDeviceInfo" size="mini">刷新设备列表</el-button>
            <exSwitch v-model="isShowWhistle" label="显示抓包日志"></exSwitch>
        </div>
        <el-row class="mt10">
            <el-col :span="24">
                <devInfo :deviceList="deviceList"></devInfo>
            </el-col>
        </el-row>
        <div class="w100" v-if="isShowWhistle">
            <whistle></whistle>
        </div>
    </div>
</template>


<script>
import whistle from "./comp/whistle"
import devInfo from "./comp/devInfo"

export default {
    components:{
        whistle,
        devInfo
    },
    data() {
        return {
            devices: "",
            deviceList: [],
            isShowWhistle : false,
        }
    },
     

    mounted() {
        this.regIpc();
        this.getDeviceInfo();
    },

    methods: {
        getDeviceInfo(){
            this.devices = "";
            this.deviceList = [];
            electron.ipcRenderer.send("doCmd",{name:"getDeviceList",cmd:"adb devices",param:{type:"adb"}})
        },
        regIpc(){
            var that = this;
            if(!window.electron){
                return;
            }
            this.registerListen(this,"getDeviceList",(res)=>{
                try{
                    if(res.isSucc){
                        // that.$message.info(res.info);
                        console.info(res.info);
                        res.info=res.info.replace(/\\r/g,"");
                        that.deviceList = [];
                        that.devices = "";
                        var lines = res.info.split("\n");
                        lines.map(row=>{
                            var item = {};
                            var arr = row.split("\t");
                            if(arr.length!=2) return;
                            if(row.match(/^.*device/)){
                                item={name:arr[0],value:arr[0],isAuth:true};
                            }else if(row.match(/^.*unauthorized/)){
                                item={name:arr[0]+"(请在手机授权)",value:arr[0],isAuth:false};
                            }
                            if(item.value){
                                that.deviceList.push(item);
                                if(!that.devices && item.isAuth) that.devices = item.value;
                                // if(that.devices.length==0) that.devices.push(item.value)
                            }
                        })
                    }else{
                        that.$message.error("指令("+res.cmd+")执行异常:"+res.info)
                    }
                }catch(ex){
                    that.$message.error("指令执行异常:"+ex.message)
                }
            });
        },
    }
}
</script>

<style lang="less" scoped>

</style>
